package com.health.dao;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.health.model.BloodSugar;

public class BloodSugarDAO
{

	private final Log				log			= LogFactory.getLog(BloodSugarDAO.class);

	private SessionFactory	sessionFactory;

	private SimpleDateFormat		formatter	= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


	public void insert(BloodSugar s) throws ParseException
	{
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		String examTimeStr = formatter.format(s.getExamTime());
		Date examTime = formatter.parse(examTimeStr);
		session.save(s);
		session.getTransaction().commit();
		session.close();
	}

	public void delete(BloodSugar s)
	{
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.delete(s);
		session.getTransaction().commit();
		session.close();
	}

	public void delete(String id)
	{
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		BloodSugar s = new BloodSugar();
		s.setId(id);
		session.delete(s);
		session.getTransaction().commit();
		session.close();
	}
	
	public void update(BloodSugar s)
	{
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.update(s);
		session.getTransaction().commit();
		session.close();
	}

	public BloodSugar selectByPK(String id)
	{
		BloodSugar s = new BloodSugar();
		Session session = null;
		session = sessionFactory.openSession();
		s = (BloodSugar) session.load(BloodSugar.class, id);
		// session.close();
		return s;
	}

	public List<BloodSugar> selectListAll(BloodSugar s)
	{
		Session session = null;
		session = sessionFactory.openSession();
		List<BloodSugar> collection = null;
		String sql = "select S from BloodSugar S where 1=1";
		if (null != s.getId() && !"".equals(s.getId().trim()))
		{
			sql += " and S.id = '" + s.getId() + "'";
		}
		else if (null != s.getUserId() && !"".equals(s.getUserId().trim()))
		{
			sql += " and S.userId = '" + s.getUserId() + "'";
		}
		else
		{
			return new ArrayList();
		}
		if (null != s.getExamTimeStart())
		{
			sql += " and S.examTime >= '"
					+ formatter.format(s.getExamTimeStart()) + " 00:00:00"
					+ "'";
		}
		if (null != s.getExamTimeEnd())
		{
			sql += " and S.examTime <= '"
					+ formatter.format(s.getExamTimeEnd()) + " 23:59:59" + "'";
		}
		if (null != s.getValue1())
		{
			sql += " and S.value >= " + s.getValue1();
		}
		if (null != s.getValue2())
		{
			sql += " and S.value <= " + s.getValue2();
		}
		if (null != s.getAtMeal() && !"".equals(s.getAtMeal()))
		{
			sql += " and S.atMeal = " + s.getAtMeal();
		}
		Query query = session.createQuery(sql);
		collection = query.list();

		session.close();
		return collection;
	}

	public List<BloodSugar> selectListPage(BloodSugar s)
	{
		int start = s.getStart();
		int limit = s.getLimit();
		Session session = null;
		session = sessionFactory.openSession();
		List<BloodSugar> collection = null;
		String sql = "select S from BloodSugar S where 1=1";
		if (null != s.getId() && !"".equals(s.getId().trim()))
		{
			sql += " and S.id = '" + s.getId() + "'";
		}
		else if (null != s.getUserId() && !"".equals(s.getUserId().trim()))
		{
			sql += " and S.userId = '" + s.getUserId() + "'";
		}
		else
		{
			return new ArrayList();
		}
		if (null != s.getExamTimeStart())
		{
			sql += " and S.examTime >= '"
					+ formatter.format(s.getExamTimeStart()) + " 00:00:00"
					+ "'";
		}
		if (null != s.getExamTimeEnd())
		{
			sql += " and S.examTime <= '"
					+ formatter.format(s.getExamTimeEnd()) + " 23:59:59" + "'";
		}
		if (null != s.getValue1())
		{
			sql += " and S.value >= " + s.getValue1();
		}
		if (null != s.getValue2())
		{
			sql += " and S.value <= " + s.getValue2();
		}
		if (null != s.getAtMeal() && !"".equals(s.getAtMeal()))
		{
			sql += " and S.atMeal = " + s.getAtMeal();
		}
		Query query = session.createQuery(sql);
		query.setFirstResult(start);
		query.setMaxResults(limit);
		collection = query.list();
		session.close();
		return collection;
	}

	/***************************************************************************
	 * getters and settres
	 **************************************************************************/
	
	public SessionFactory getSessionFactory()
	{
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory)
	{
		this.sessionFactory = sessionFactory;
	}

}
